﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks;
using System.Data.SQLite;
using System.IO;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Dwgc;

namespace Dwg_output
{
    class Read_CXDatebase
    {   
        //产品数据库
        private  static  int ins_foot1 = 11;
        private  static  int outs_foot1 = 12;
        private  static int ins_foot2 = 13;
        private  static int outs_foot2 = 14;
        private  static int power_foot = 15;
        private static int eq_type= 17;



        //用户尾号数据库
        private int base_B_IO = 12;//数据库定位
        private int signal_power = 21;
        private int safe_relay_num = 20;
        private int termail = 16;

        private int Safe_L = 300;
        private int sign_type = 3;
        private int io_sign_type = 10;
        private int configuration = 11;
        private int safe_relay = 19;

        private static  bool isFile(string _Path)
        {
            if (File.Exists(_Path))
                return true;
            else
                return false;

        }
        private static bool Open_CXDatebase()
        {
            if (isFile(@"../../Source/CXDatabase.db") == false)
            {
                MessageBox.Show("数据库缺失，请查看文件夹");
                return false;
            }
            else
            {
                return true;
            }
        }
        public static List<string> Read_equipment(string eqName,string sign_type, string io_sign_type,string configuration)
        {

            if (Open_CXDatebase() == false|| eqName.Equals(""))
            {
                return null;
            }
            string path_db = @"../../Source/CXDatabase.db";
            List<string> foot_conn= Wire_ReadSQL.Read_Equip_Foot(path_db, eqName, sign_type, io_sign_type, configuration);

            return foot_conn;
           
        }
      

        public static void Write_equipment(List<string> foot_conn, Database destDb, Point3d point3D, bool double_foot)
        {

          

            string[] Str_ins_foot1 = Split_String(foot_conn[ins_foot1]);
            string[] Str_outs_foot1 = Split_String(foot_conn[outs_foot1]);
            string[] Str_ins_foot2 = Split_String(foot_conn[ins_foot2]);
            string[] Str_outs_foot2 = Split_String(foot_conn[outs_foot2]);
            string[] Str_power_foot = Split_String(foot_conn[power_foot]);


            for (int i = 0; i < Str_ins_foot1.Count(); i++)
            {
                if (double_foot == false|| Str_ins_foot2.Count() <=1)
                {

                    Creat_text_Dwg.ctrateText(Str_ins_foot1[i], destDb
                             , point3D.Add(new Vector3d(0, -26 * i, 0)), 20, 0, 0);


                    if (i < Str_outs_foot1.Count())
                    {
                        Creat_text_Dwg.ctrateText(Str_outs_foot1[i], destDb
                                      , point3D.Add(new Vector3d(269 - 1.4, -26 * i, 0)), 20, 0, 0);
                    }

                }

                else
                {
                    if (i >= Str_ins_foot2.Count())
                    { continue; }

                  
                        Creat_text_Dwg.ctrateText(Str_ins_foot2[i], destDb
                                , point3D.Add(new Vector3d(0, -26 * i, 0)), 20, 0, 0);


                    if (i >= Str_outs_foot2.Count())
                    { continue; }
                    Creat_text_Dwg.ctrateText(Str_outs_foot2[i], destDb
                                           , point3D.Add(new Vector3d(269 - 1.4, -26 * i, 0)), 20, 0, 0);


                

                }




                if (i >= Str_power_foot.Count())
                { continue; }
                Creat_text_Dwg.ctrateText(Str_power_foot[i], destDb
                                  , point3D.Add(new Vector3d(269-1.4,-26*2 -26 * i, 0)), 20, 0, 0);

            }




        }

        //安全栅引脚输出
        public string SafeEquip(List<string> eqName, Database destDb, Point3d point3D, bool double_foot)
        {

            List<string> foot_conn  =  Read_CXDatebase.Read_equipment(eqName[safe_relay], eqName[sign_type], eqName[io_sign_type], eqName[configuration]);
            if (foot_conn.Count==0)
            {
                return eqName[safe_relay];
            }

            Read_CXDatebase.Write_equipment(foot_conn, destDb, point3D, double_foot);
            return null;

        }

        public string Read_equip_type(List<string> eqName)
        {
            string str_eq_type;
          
            List<string> eq_type_L = Read_CXDatebase.Read_equipment(eqName[safe_relay], eqName[sign_type], eqName[io_sign_type], eqName[configuration]);

            if (eq_type_L.Count()>0)
            {
                str_eq_type =  eq_type_L[eq_type];
                return str_eq_type;//即使没有应用标记也会返回....
            }
            else
            {
                return "";
            }

        }


        private static string[] Split_String(string input)
        {


            string[] parts = input.Split('|');


            return parts;

        }


    }
}
